package com.bxd.baodan.list;

import android.content.Context;
import android.content.Intent;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.google.gson.reflect.TypeToken;
import com.bxd.baodan.App;
import com.bxd.baodan.R;
import com.bxd.baodan.activity.AdviserActivity;
import com.bxd.baodan.activity.LoginActivity;
import com.bxd.baodan.activity.QuestionDetailActivity;
import com.bxd.baodan.app.LoginInfo;
import com.bxd.baodan.base.BaseListFragment;
import com.bxd.baodan.item.QuestionItem;
import com.bxd.baodan.util.ImageUtil;
import com.bxd.baodan.util.LogUtil;
import com.bxd.baodan.widget.GlideCircleTransform;
import com.bxd.baodan.widget.LineLayout;
import com.tencent.TIMConversationType;
import com.tencent.qcloud.timchat.ui.ChatActivity;
import com.umeng.analytics.MobclickAgent;
import com.bxd.baodan.adapter.ViewHolder;

import java.lang.reflect.Type;
import java.util.Arrays;
import java.util.List;

/**
 * Created by lizhichao on 17/7/23.
 */

public class AdviserWenwenFragment extends BaseListFragment<QuestionItem> {
    private Context context;

    @Override
    protected void preDone(View v) {
        context = getActivity();
        getData();
    }

    @Override
    protected int getFragmentLayout() {
        return R.layout.pull_refresh_layout;
    }

    @Override
    protected int getItemLayout() {
        return R.layout.question_detail_item_old;
    }

    @Override
    protected Type getTypeToken() {
        return new TypeToken<List<QuestionItem>>() {
        }.getType();
    }

    @Override
    protected void setUsualNetUtil() {


    }

    @Override
    protected void convert(ViewHolder holder, final QuestionItem item) {

        final TextView answer_adress;
        TextView tvQuestionContent;
        TextView tvAnswerCount;
        TextView text;
        LineLayout keyLabel;
        LinearLayout answerLayout;
        ImageView ivAnswerAvatar;
        TextView tvAnswerName;
        TextView tvAnswerContent;

        TextView tv_time;
        // TODO: 2017/3/27  //verify_level  company  chat_online 添加holder
        TextView company;
        TextView verify_level;
        TextView chat;
        ImageView verify_icon;

        TextView asker;
        ImageView asker_img;
        TextView ask_time;
        View main;


        // 问题
        main = holder.getView(R.id.main);
        keyLabel = (LineLayout) holder.getView(R.id.keyLabel);
        text = (TextView) holder.getView(R.id.text);
        answer_adress = (TextView) holder.getView(R.id.answer_adress);
        tvQuestionContent = (TextView) holder.getView(R.id.question_content);
        tvAnswerCount = (TextView) holder.getView(R.id.answer_count);
        answerLayout = (LinearLayout) holder.getView(R.id.answer_layout);
        ivAnswerAvatar = (ImageView) holder.getView(R.id.answer_avatar);
        tvAnswerName = (TextView) holder.getView(R.id.answer_name);
        tv_time = (TextView) holder.getView(R.id.tv_time);

        // TODO: 2017/3/27  //verify_level  company  chat_online 添加holder
        verify_level = (TextView) holder.getView(R.id.verify_level);
        company = (TextView) holder.getView(R.id.answer_company);
        chat = (TextView) holder.getView(R.id.chat_status);
        verify_icon = (ImageView) holder.getView(R.id.verify_icon);
        tvAnswerContent = (TextView) holder.getView(R.id.answer_content);


        ask_time = (TextView) holder.getView(R.id.ask_time);
        asker_img = (ImageView) holder.getView(R.id.asker_img);
        asker = (TextView) holder.getView(R.id.asker);


        main.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MobclickAgent.onEvent(App.getContext(), "v5_5_1_SY_zgw_zy_wt");

                Intent intent = new Intent(App.getContext(),
                        QuestionDetailActivity.class);
                intent.putExtra("item", item);
                context.startActivity(intent);
            }
        });


        Glide.with(context).
                load(item.getU_avatar()).
                placeholder(R.drawable.empty_person).
                error(R.drawable.empty_person)
                .transform(new GlideCircleTransform(context))
                .into(asker_img);
        asker.setText(item.getU_nick());
        ask_time.setText(item.getQ_create_date());


        // TODO: 2017/3/28 usr信息
        tvQuestionContent.setText(item.getQ_title());
        // 判断是否有人回答
        if (item.getQ_answer_count() == 0) {
            tvAnswerCount.setText("等待回答");
            text.setVisibility(View.GONE);
        } else {
            tvAnswerCount.setText(item.getQ_answer_count() + "");
            text.setVisibility(View.VISIBLE);
        }
        // 标签
        keyLabel.removeAllViews();
        String label = item.getKeyLabel();
        int[] drawables = {R.drawable.round_corner_green, R.drawable.round_corner_blue, R.drawable.round_corner_yellow};
        int[] colors = {R.color.green_lab, R.color.blue_lab, R.color.yellow_lab};
        if (label != null && !"".equals(label)) {
            String[] tagStrings = label.split(",");
            for (int i = 0; i < tagStrings.length; i++) {
                if ("".equals(tagStrings[i])) {
                    continue;
                }
                if (i > 2) {
                    break;
                }
                View myTagView = LayoutInflater.from(context).inflate(
                        R.layout.route_place_tag, null);
                LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                        LinearLayout.LayoutParams.MATCH_PARENT,
                        LinearLayout.LayoutParams.WRAP_CONTENT);
                params.gravity = Gravity.CENTER_VERTICAL;
                TextView ttm = (TextView) myTagView.findViewById(R.id.text);
                ttm.setTextColor(context.getResources().getColor(colors[i % 3]));
                ttm.setBackgroundResource(drawables[i % 3]);
                ttm.setText(tagStrings[i]);
                keyLabel.addView(myTagView, params);
            }
        }


        // 判断是否有推荐的回答
        if (item.getQa_answer_info() != null
                && !"".equals(item.getQa_answer_info())) {
            answerLayout.setVisibility(View.VISIBLE);

            ivAnswerAvatar.setImageResource(R.drawable.empty_person);
            ImageUtil.setAvatarToRounderCorner(context, item
                            .getQa_answer_info().getAvatar(), ivAnswerAvatar,
                    true);

            ivAnswerAvatar.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    openActivity(AdviserActivity.class, new String[]{"params"}, new String[]{item.getQa_answer_info().getU_id()});
                }
            });

            tvAnswerName.setText(item.getQa_answer_info().getName());
            answer_adress.setText(item.getQa_answer_info().getCity());
            // 判断回答的内容的长度
            try {

                if (item.getQa_answer_info().getQa_title().length() >= 36) {
                    String content = item.getQa_answer_info().getQa_title()
                            .substring(0, 35).trim();
                    tvAnswerContent.setText(content + "......");
                } else {
                    tvAnswerContent.setText(item.getQa_answer_info()
                            .getQa_title());
                }
            } catch (Exception e) {
                answerLayout.setVisibility(View.GONE);
            }


            //answer信息
            // TODO: 2017/3/27 item 添加字段 需要
            String verify = "";
            int level = item.getQa_answer_info().ua_title;
            int verColor = R.color.white;
            int drawableLeft = R.color.transparent;



            switch (level) {
                case 0:
                    break;
                case 4:
                    verify = "保险管家";
                    verColor = R.color.guanja;
                    drawableLeft = R.drawable.round_corner_yellow;
                    break;
                case 2:
                    verify = "保险精英";
                    verColor = R.color.jingying;
                    drawableLeft = R.drawable.round_corner_blue_jingying;
                    break;
                case 1:
                    verify = "保险达人";
                    verColor = R.color.daren;
                    drawableLeft = R.drawable.round_corner_green;
                    break;
                case 3:
                    verify = "保险专家";
                    verColor = R.color.zhuanjia;
                    drawableLeft = R.drawable.round_corner_red_zhuanjia;
                    break;
                default:
                    break;
            }



            verify_level.setBackground(context.getResources().getDrawable(drawableLeft));
            verify_level.setText(verify);
            verify_level.setTextColor(context.getResources().getColor(verColor));

            int chatStatus = item.getQa_answer_info().ue_onlineStatus;
            chat.setBackground(context.getResources().getDrawable(chatStatus == 0 ? R.drawable.bg_gray_stroke : R.drawable.round_corner_yellow));
            chat.setCompoundDrawablesWithIntrinsicBounds(context.getResources().getDrawable(chatStatus == 0 ? R.drawable.one_by_one_gray : R.drawable.one_by_one), null, null, null);
            chat.setTextColor(context.getResources().getColor(chatStatus == 0 ? R.color.gray : R.color.yellow_lab));

            chat.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    LogUtil.e("一对一聊天  -->v5_1_0_ww_1v1_click");
                    MobclickAgent.onEvent(context, "v5_1_0_ww_1v1_click");
                    if (LoginInfo.getInstance(App.getContext()).isLogin()) {
                        ChatActivity.navToChat(context, item.getQa_answer_info().getU_id() + "", TIMConversationType.C2C);
                    } else {
                        context.startActivity(new Intent(context, LoginActivity.class));

                    }
                }
            });


            // TODO: 2017/3/27 身份认证要改
            if (item.getQa_answer_info().agent_mark != null && item.getQa_answer_info().agent_mark.length > 0
                    && Arrays.asList(item.getQa_answer_info().agent_mark).contains("6")) {
                verify_icon.setVisibility(View.VISIBLE);
            } else {
                verify_icon.setVisibility(View.GONE);
            }

            // TODO: 2017/3/27 公司要改
            company.setText(item.getQa_answer_info().companyname);

        } else {
            answerLayout.setVisibility(View.GONE);
        }


    }

    @Override
    protected String getArrayStr(String json_data) {
        return "list";
    }
}
